﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;
using System.Drawing;

namespace PET.Controllers
{
    /// <summary>
    /// 宠物商店
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class StoreController : ControllerBase
    {
        private readonly PetDbContext db;
        public StoreController(PetDbContext _context)
        {
            db = _context;

        }

        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Pet_Store> GetAll()
        {
            return db.Pet_Store.OrderByDescending(s => s.Id).ToList();
        }

        [HttpGet]
        public List<Pet_Store> GetBySelect([FromBody] decimal Price)
        {
            List<Pet_Store> store = new List<Pet_Store>();

            if (Price > 0)
            {
                store = db.Pet_Store.Where(s => s.Price == Price).OrderBy(s => s.Price).ToList();
            }

            return store;
        }

        /// <summary>
        /// 增加信息
        /// </summary>
        /// <param name="adopt"></param>
        /// <returns></returns>

        [HttpPost]
        public Result Add([FromBody] Pet_Store store)
        {
            Result result = new Result();
            db.Pet_Store.Add(store);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_Store? store = db.Pet_Store.Where(w => w.Id == id).SingleOrDefault();
            if (store != null)
                db.Pet_Store.Remove(store);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        [HttpPut]
        public Result Up([FromBody] Pet_Store store)
        {
            Result result = new Result();
            db.Pet_Store.Update(store);
            result.Data = db.SaveChanges();
            return result;
        }

        [HttpDelete("{Id}")]
        public bool Delete(Guid Id)
        {
            Pet_Store? store = new Pet_Store();
            store = db.Pet_Store.Where(s => s.Id == Id).FirstOrDefault();

            int result = 0;
            if (store != null)
            {
                store.SellingTime = DateTime.Now;
                db.Pet_Store.Update(store);
                result = db.SaveChanges();

            }


            return result > 0 ? true : false;
        }
    }
}
